<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="供应商id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入供应商id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="商品名称" prop="goodName">
        <el-input v-model="queryParams.goodName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="供应商名称" prop="supplierName" label-width="100px">
        <!-- <el-input v-model="queryParams.supplierName" placeholder="供应商名称" clearable @keyup.enter.native="handleQuery" /> -->
        <el-select v-model="queryParams.supplierName" filterable placeholder="请选择供应商" @keyup.enter.native="handleQuery">
          <el-option v-for="item in userLists" :key="item.id" :label="item.storeName" :value="item.storeName">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="批发价" prop="tradePrice">
        <el-input
          v-model="queryParams.tradePrice"
          placeholder="请输入批发价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="零售价" prop="retailPrice">
        <el-input
          v-model="queryParams.retailPrice"
          placeholder="请输入零售价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建价格" prop="createUser">
        <el-input
          v-model="queryParams.createUser"
          placeholder="请输入创建价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="区域" prop="region">
        <el-select v-model="queryParams.region" placeholder="请选择" clearable>
          <el-option v-for="item in goodAreas" :key="item.id" :label="item.cityName" :value="item.id"></el-option>
        </el-select>
      </el-form-item> -->

      <!-- <el-form-item label="" prop="classOneId">
        <el-input
          v-model="queryParams.classOneId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="classTwoId">
        <el-input
          v-model="queryParams.classTwoId"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="一级分类" prop="classOneId">
        <el-select v-model="queryParams.classOneId" clearable placeholder="请选择" @change="getline">
          <el-option v-for="item in goodclassifyListOne" :key="item.deptName" :label="item.deptName"
            :value="item.id"></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="二级分类" prop="classTwoId">-->
<!--        <el-select v-model="queryParams.classTwoId" clearable placeholder="请选择" @change="getlineTwo">-->
<!--          <el-option v-for="item in goodclassifyListTwo" :key="item.deptName" :label="item.deptName"-->
<!--            :value="item.id"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="三级分类" prop="classTwoId">-->
<!--        <el-select v-model="queryParams.classThreeId" clearable placeholder="请选择">-->
<!--          <el-option v-for="item in goodclassifyListThree" :key="item.deptName" :label="item.deptName"-->
<!--            :value="item.id"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="isDisplayed">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:good:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:good:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:good:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:good:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodList" @selection-change="handleSelectionChange" height="680px" row-key="id"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="" align="center" prop="id" /> -->
      <!-- <el-table-column label="供应商id" align="center" prop="userId" /> -->
      <el-table-column label="商品名称" align="center" prop="goodName" width="130" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" width="120" />

      <!-- <el-table-column label="描述" align="center" prop="goodDesc" :show-overflow-tooltip='true'  /> -->
      <el-table-column label="批发价" align="center" prop="tradePrice" />
      <el-table-column label="零售价" align="center" prop="retailPrice" />
      <el-table-column label="会员价" align="center" prop="memberPrice" />
      <!-- <el-table-column label="创建价格" align="center" prop="createUser" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="区域" align="center" prop="regionName" :show-overflow-tooltip="true" />
      <el-table-column label="累计库存数量" align="center" prop="grandCounts" />
      <el-table-column label="现有库存数量" align="center" prop="counts" />
      <el-table-column label="总销售数量" align="center" prop="totalSellCount" />
      <el-table-column label="商品图片" align="center" prop="goodImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.goodImg" :width="50" :height="50" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="详情" align="center" prop="goodDetail" :show-overflow-tooltip='true' />
      <el-table-column label="说明" align="center" prop="goodText" :show-overflow-tooltip='true' /> -->
      <el-table-column label="二维码" align="center" prop="qrCodeUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.qrCodeUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 1" style="color: #FFCC22;">待审核</span>
          <span v-if="scope.row.status == 2" style="color: green;">审核通过</span>
          <span v-if="scope.row.status == 3" style="color: red;">审核不通过</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="" align="center" prop="classOneId" />
      <el-table-column label="" align="center" prop="classTwoId" /> -->
      <el-table-column label="一级分类" align="center" prop="classOneName" />
<!--      <el-table-column label="二级分类" align="center" prop="classTwoName" />-->
<!--      <el-table-column label="三级分类" align="center" prop="classThreeName" />-->
      <el-table-column label="是否上架" align="center" prop="isInitiate">
        <template slot-scope="scope">
          <span v-if="scope.row.isInitiate == 0">已上架</span>
          <span v-if="scope.row.isInitiate == 1">未上架</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="库存版本号" align="center" prop="version" /> -->
      <el-table-column label="操作" align="center" width="250" fixed="right">
        <template slot-scope="scope">



          <template v-if="scope.row.status == 2">
            <el-button size="mini" type="text" @click="handleUp(scope.row)" v-hasPermi="['system:good:upshelf']"
              v-if="scope.row.isInitiate == 1">上架</el-button>

            <el-button size="mini" type="text" @click="handleDown(scope.row)" v-hasPermi="['system:good:downshelf']"
              v-if="scope.row.isInitiate == 0">下架</el-button>
          </template>

          <!-- <el-button
            size="mini"
            type="text"
            @click="handleBuy(scope.row)"
          >采购</el-button> -->

          <template>
            <el-button size="mini" type="text" @click="handlePass(scope.row)" v-if="scope.row.status == 1"
              v-hasPermi="['system:good:by']">审核通过</el-button>

            <el-button size="mini" type="text" @click="handleNopass(scope.row)" v-if="scope.row.status == 1"
              v-hasPermi="['system:good:pass']">审核不通过</el-button>
          </template>


          <el-button size="mini" type="text" @click="checkDetail(scope.row, false)"
            v-hasPermi="['system:good:edit']">详情</el-button>

          <el-button v-if="scope.row.parentId == 0" size="mini" type="text" @click="handleAdd(scope.row)"
                     v-hasPermi="['system:good:add']">添加规格</el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row, true)"
            v-hasPermi="['system:good:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['system:good:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改供应商商品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px" :disabled="false">
        <el-row v-if="form.parentId === 0 || !form.parentId ">
          <el-col :span="12">
            <el-form-item label="一级分类" prop="classOneId">
              <el-select v-model="form.classOneId" placeholder="请选择" @change="getline">
                <el-option v-for="item in goodclassifyListOne" :key="item.deptName" :label="item.deptName"
                  :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="区域" prop="regionArr">
              <el-select v-model="form.regionArr" multiple placeholder="请选择" @change="$forceUpdate()">
                <el-checkbox v-model="form.checkAll" @change="selectAll">全选</el-checkbox>
                <el-option v-for="item in goodAreas" :key="item.id" :label="item.cityName" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="二级分类" prop="classTwoId">-->
<!--              <el-select v-model="form.classTwoId" placeholder="请选择" @change="getlineTwo">-->
<!--                <el-option v-for="item in goodclassifyListTwo" :key="item.deptName" :label="item.deptName"-->
<!--                           :value="item.id"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="三级分类" prop="classThreeId">-->
<!--              <el-select v-model="form.classThreeId" placeholder="请选择" @change="getlineThree">-->
<!--                <el-option v-for="item in goodclassifyListThree" :key="item.deptName" :label="item.deptName"-->
<!--                  :value="item.id"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->

        <el-row>
          <el-col :span="12">
            <el-form-item label="商品名称" prop="goodName">
              <el-input v-model="form.goodName" placeholder="请输入名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- :filter-method="formFilterMethod" -->
            <el-form-item label="商品模板">
              <el-select v-model="goodNameMuban" filterable placeholder="请选择商品模板" style="width: 97.5%;"
                @change="getlineFour">
                <el-option v-for="(item, index) in goodclassifyListFour" :key="index" :label="item.goodName"
                  :value="item.goodName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="商品图片" prop="goodImg">
          <image-upload v-model="form.goodImg" :limit="1" />
        </el-form-item>
        <el-form-item label="详情图片" prop="goodDetail">
          <image-upload v-model="form.goodDetail" />
        </el-form-item>
        <!-- <el-form-item label="详情">
          <editor v-model="form.goodDetail" :min-height="192" />
        </el-form-item> -->
        <el-form-item label="详情介绍" prop="goodText">
          <el-input v-model="form.goodText" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="批发价" prop="tradePrice">
              <el-input v-model="form.tradePrice" placeholder="请输入批发价" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="零售价" prop="retailPrice">
              <el-input v-model="form.retailPrice" placeholder="请输入零售价" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="会员价" prop="memberPrice">
              <el-input v-model="form.memberPrice" placeholder="请输入会员价" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="数量" prop="counts">
              <el-input v-model="form.counts" placeholder="请输入数量" onkeyup="value=value.replace(/^(0+)|[^\d]+/g,'')"
                @change="countsChange" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="单位[计量]" prop="units">
          <el-input v-model="form.units" placeholder="请输入单位" />
        </el-form-item>
        <div v-for="(item, i) in form.paramsNewArr">
          <el-row>
            <el-col :span="10">
              <el-form-item label="参数名称" :prop="`paramsNewArr[${i}].paramsName`"
                :rules="[{ required: true, message: '输入参数名称', trigger: 'change' }]">
                <el-input v-model="item.paramsName" placeholder="请输入单位" />
              </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item label="参数内容">
                <el-input v-model="item.paramsValue" placeholder="请输入单位" />
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <div>
                <i style="cursor: pointer; margin-left: 6px;margin-top: 5px;font-size: 20px;"
                  v-if="form.paramsNewArr.length > 1" class="el-icon-remove-outline" @click="del(i)"></i>
                <i style="cursor: pointer; margin-left: 6px;margin-top: 5px;font-size: 20px;"
                  class="el-icon-circle-plus-outline" @click="add"></i>
              </div>
            </el-col>

          </el-row>
        </div>


        <!-- <el-form-item label="说明" prop="goodText">
          <el-input v-model="form.goodText" type="textarea" placeholder="请输入内容" />
        </el-form-item> -->
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item> -->
        <!-- <el-form-item label="" prop="classOneName">
          <el-input v-model="form.classOneName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="" prop="classTwoName">
          <el-input v-model="form.classTwoName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="库存版本号" prop="version">
          <el-input v-model="form.version" placeholder="请输入库存版本号" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="isEdit == true">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <el-dialog :title="buytitle" :visible.sync="buyopen" width="500px" append-to-body>
      <el-form ref="buyform" :model="buyform" :rules="buyrules" label-width="80px">
        <el-form-item label="采购数量" prop="counts">
          <el-input v-model="buyform.counts" placeholder="请输入采购数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="buySubmitForm">确 定</el-button>
        <el-button @click="buyCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGood, getGood, delGood, addGood, updateGood, buyGood, hierarchy,listSupplierName } from "@/api/fx/suppliergood";
import { listGoodclassify } from "@/api/fx/goodclassify";
import { listArea } from "@/api/fx/goodArea";
import { listUser } from "@/api/fx/user";
// import { listSupplierName } from "@/api/fx/join";
import { fourSelectModel } from "@/api/fx/model";
import Cookies from "js-cookie";

export default {
  name: "Good",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商商品表格数据
      goodList: [],
      goodAreas: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 采购弹出层标题
      buytitle: "",
      // 采购是否显示弹出层
      buyopen: false,
      //是否运行新增
      isDisplayed: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 99,
        userId: null,
        goodName: null,
        goodText: null,
        tradePrice: null,
        retailPrice: null,
        createUser: null,
        regionArr: [],
        region: null,
        grandCounts: null,
        counts: null,
        totalSellCount: null,
        goodImg: null,
        goodDetail: null,
        goodText: null,
        qrCodeUrl: null,
        status: null,
        classOneId: null,
        classTwoId: null,
        classThreeId: null,
        createrName: null,
        classOneName: null,
        classTwoName: null,
        classThreeName: null,


        isInitiate: null,
        version: null
      },
      goodNameMuban: '',
      userParams: {
        pageNum: 1,
        pageSize: 10000,
        roleType: 3,
        userType: 3,
      },
      // 表单参数
      form: {
        regionArr: [],
        checkAll: false,
        memberPrice: null,
        units: null,
        paramsNewArr: [
          { paramsName: '品牌', paramsValue: '' },
          { paramsName: '规格', paramsValue: '' }
        ],

      },
      isEdit: true,
      goodclassifyList: [],
      goodclassifyListOne: [],
      goodclassifyListTwo: [],
      goodclassifyListThree: [],
      goodclassifyListFour: [],
      goodclassifyListFours: [],
      // 表单校验
      rules: {
        goodName: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        // goodDesc: [
        //   { required: true, message: "描述不能为空", trigger: "blur" }
        // ],
        tradePrice: [
          { required: true, message: "批发价不能为空", trigger: "blur" }
        ],
        retailPrice: [
          { required: true, message: "零售价不能为空", trigger: "blur" }
        ],
        counts: [
          { required: true, message: "数量不能为空", trigger: "blur" }
        ],
        // goodImg: [
        //   { required: true, message: "商品图片不能为空", trigger: "blur" }
        // ],
        // goodDetail: [
        //   { required: true, message: "详情图片不能为空", trigger: "blur" }
        // ],
        classOneId: [
          { required: true, message: "一级分类不能为空", trigger: "blur" }
        ],
        classTwoId: [
          { required: true, message: "二级分类不能为空", trigger: "blur" }
        ],
        classThreeId: [
          { required: true, message: "三级分类不能为空", trigger: "blur" }
        ],
        regionArr: [
          { required: true, message: "区域不能为空", trigger: "blur" }
        ],
        units: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ]
      },
      // 采购表单参数
      buyform: {},
      // 采购表单校验
      buyrules: {
        counts: [
          { required: true, message: "采购数量不能为空", trigger: "blur" }
        ],
      },
      userLists: [],
      goodsImg: null
    };
  },
  created() {
    this.getList();
    this.getlistGoodclassify();
    this.getGoodlistArea();

    const roles = JSON.parse(Cookies.get("roles"));
    console.log(roles, 'roles``````');

    roles.forEach(item => {
      if (item == 'supplier') {
        this.isDisplayed = true
      }
    })
    this.getUserList()
  },
  methods: {
    //添加评价内容
    add() {
      this.form.paramsNewArr.push({ paramsName: '', paramsValue: '' })
    },
    //删除评价内容
    del(i) {
      this.form.paramsNewArr.splice(i, 1)
    },
    getUserList() {
      // console.log("走了")
      listSupplierName().then(res => {
        console.log("走了", res, res.data)

        if (res.code === 200)
          this.userLists = res.data
      })
      // listUser(this.userParams).then(res => {
      //   if (res.code === 200)
      //     this.userLists = res.rows
      // })

    },
    getGoodlistArea() {
      listArea(this.queryParams).then(response => {
        this.goodAreas = response.rows;
        console.log(this.goodAreas);
      });
    },

    //全选按钮
    selectAll() {
      // 当全选复选框状态变化时，更新选择项的状态
      if (this.form.checkAll) {
        this.form.regionArr = this.goodAreas.map(item => item.id);
      } else {
        this.form.regionArr = [];
      }

    },

    countsChange(v) {
      this.form.counts = v
    },

    getlistGoodclassify() {
      listGoodclassify(this.queryParams).then(response => {
        this.goodclassifyList = response.rows;
        this.goodclassifyListOne = this.goodclassifyList.filter(item => item.tier == 0);
      });
    },
    getline(row) {
      this.goodclassifyListTwo = this.goodclassifyList.filter(item => item.parentId == parseInt(row));
      // this.form.classTwoId = '';
      // this.form.classThreeId = '';
    },
    regionChange(value) {
      this.form.regionArr = value
    },
    getlineTwo(row) {
      this.goodclassifyListThree = this.goodclassifyList.filter(item => item.parentId == parseInt(row));
      this.form.classThreeId = '';
    },

    getlineThree(row) {
      let oneName = this.goodclassifyListOne.filter(item => item.id == this.form.classOneId)[0].deptName;
      // let twoName = this.goodclassifyListTwo.filter(item => item.id == this.form.classTwoId)[0].deptName;
      // let threeName = this.goodclassifyListThree.filter(item => item.id == row)[0].deptName;
      let regionArr = this.form.regionArr
      let oneId = this.form.classOneId
      // let twoId = this.form.classTwoId

      let query = {
        hierarchy: row
      }
      hierarchy(query).then(res => {
        console.log(res, '根据最后一级id查询模板');
        if (res.code == 200) {
          this.goodsImg = res.data.goodImg
          this.form.goodName = res.data.goodName
          this.form.goodImg = this.goodsImg
          this.goodclassifyListFour = [...res.data.modelList]
          this.goodclassifyListFours = [...res.data.modelList]
          this.form.classOneId = oneId
          // this.form.classTwoId = twoId
          // this.form.classThreeId = row
          this.form.regionArr = regionArr
          this.form.classOneName = oneName
          this.form.classTwoName = twoName
          this.form.classThreeName = threeName
        }
      })
    },
    getlineFour(row) {
      let detail = this.goodclassifyListFour.filter(item => item.goodName == row)
      if (detail[0].specificationsList && detail[0].specificationsList.length != 0) {
        detail[0].paramsNewArr = []
        detail[0].specificationsList.map(el => {
          detail[0].paramsNewArr.push({
            paramsName: el.split(',')[0],
            paramsValue: el.split(',')[1],
          })
        })
      } else {
        detail[0].paramsNewArr = [{
          paramsName: '品牌',
          paramsValue: ''
        }, {
          paramsName: '规格',
          paramsValue: ''
        }]
      }
      let newForm = JSON.parse(JSON.stringify(this.form))
      this.form = Object.assign(newForm, detail[0])
    },
    formFilterMethod(row) {
      let list = []
      this.goodclassifyListFours.forEach(item => {
        if (item.goodName.indexOf(row) !== -1) {
          list.push(item)
        }
      })
      this.goodclassifyListFour = list
      // if (list.length == 0) {
      //   this.goodclassifyListFour = [...this.goodclassifyListFours]
      // } else {
      // }
      this.$set(this.form, 'goodName', row)
      let newForm = {
        goodImg: this.goodsImg,
        goodDetail: '',
        goodText: '',
        tradePrice: '',
        retailPrice: '',
        memberPrice: ''
      }
      Object.assign(this.form, newForm)
    },

    /**会员价修改 */
    memberPriceChange(v) {
      console.log(88888888, v);

      this.form.memberPrice = v
    },

    /** 查询供应商商品列表 */
    getList() {
      this.loading = true;
      listGood(this.queryParams).then(response => {
        this.goodList = this.handleTree(response.rows, "id");
        this.total = response.total;
        this.loading = false;
      });
    },
    buyCancel() {
      this.buyopen = false;
      this.buyform = {};
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: null,
        userId: null,
        goodName: null,
        goodText: null,
        tradePrice: null,
        retailPrice: null,
        createTime: null,
        createUser: null,
        remark: null,
        region: null,
        regionArr: [],
        grandCounts: null,
        counts: null,
        totalSellCount: null,
        goodImg: null,
        goodDetail: null,
        goodText: null,
        qrCodeUrl: null,
        status: null,
        classOneId: null,
        classTwoId: null,
        classThreeId: null,
        classOneName: null,
        classTwoName: null,
        classThreeName: null,
        delFlag: null,
        isInitiate: null,
        version: null,
        units: null,
        paramsNewArr: [
          { paramsName: '品牌', paramsValue: '' },
          { paramsName: '规格', paramsValue: '' }
        ],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.isEdit = true;
      this.open = true;
      this.title = "添加供应商商品";
      if(row){
        this.form.parentId = row.id;
        this.form.classOneId = row.classOneId;
        this.form.region = row.region;
        this.form.regionName = row.regionName;
        this.form.regionArr = row.regionArr;
        this.title = "添加商品规格子商品";
      }
    },
    /** 上架按钮操作 */
    handleUp(row) {
      const goodid = row.id || this.ids;
      let obj = {
        id: goodid,
        isInitiate: 0
      }
      updateGood(obj).then(response => {
        this.$modal.msgSuccess("已上架");
        this.getList();
      });
    },
    /** 下架按钮操作 */
    handleDown(row) {
      const goodid = row.id || this.ids;
      let obj = {
        id: goodid,
        isInitiate: 1
      }
      updateGood(obj).then(response => {
        this.$modal.msgSuccess("已下架");
        this.getList();
      });
    },
    /** 采购按钮操作 */
    handleBuy(row) {
      this.buyopen = true;
      this.buyform.goodIds = row.id || this.ids;
    },
    /** 审核通过操作 */
    handlePass(row) {
      const goodid = row.id || this.ids;
      let obj = {
        id: goodid,
        status: 2
      }
      updateGood(obj).then(response => {
        this.$modal.msgSuccess("审核通过");
        this.getList();
      });
    },
    /** 审核不通过操作 */
    handleNopass(row) {
      const goodid = row.id || this.ids;
      let obj = {
        id: goodid,
        status: 3
      }
      updateGood(obj).then(response => {
        this.$modal.msgSuccess("审核不通过");
        this.getList();
      });
    },

    checkDetail(row, stateParam) {
      this.isEdit = stateParam;
      this.handleUpdate(row, stateParam);
    },

    /** 修改按钮操作 */
    handleUpdate(row, stateParam) {
      this.isEdit = stateParam;
      this.reset();
      const id = row.id || this.ids
      getGood(id).then(response => {
        if (response.data.specificationsList && response.data.specificationsList.length != 0) {
          response.data.paramsNewArr = []
          response.data.specificationsList.map(el => {
            response.data.paramsNewArr.push({
              paramsName: el.split(',')[0],
              paramsValue: el.split(',')[1],
            })
          })
        } else {
          response.data.paramsNewArr = [
            { paramsName: '品牌', paramsValue: '' },
            { paramsName: '规格', paramsValue: '' }
          ]
        }
        this.form = response.data;
        console.log(this.form, 'this.form');

        this.goodclassifyListOne = this.goodclassifyList.filter(item => item.tier == 0);
        this.goodclassifyListTwo = this.goodclassifyList.filter(item => item.tier == 1);
        this.goodclassifyListThree = this.goodclassifyList.filter(item => item.tier == 2);
        this.form.classOneId = parseInt(this.form.classOneId);
        // this.form.classTwoId = parseInt(this.form.classTwoId)
        // this.form.classThreeId = parseInt(this.form.classThreeId)

        this.form.regionArr = this.form.region.split(',').map(Number)
        this.open = true;
        this.title = "修改供应商商品";
      });
    },
    /** 提交按钮 */
    transformFormData(references) {
      return references.map(ref => `${ref.paramsName},${ref.paramsValue || ' '}`).join(';');
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.classOneName = this.goodclassifyList.filter(item => item.id == this.form.classOneId)[0].deptName;
          // this.form.classTwoName = this.goodclassifyList.filter(item => item.id == this.form.classTwoId)[0].deptName;
          // this.form.classThreeName = this.goodclassifyList.filter(item => item.id == this.form.classThreeId)[0].deptName;

          this.form.region = this.form.regionArr.join(',')
          this.form.specifications = this.transformFormData(this.form.paramsNewArr)
          console.log(this.form, 'this.form```````````````');

          if (this.form.id != null) {
            updateGood(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              // this.$router.go()
            });
          } else {
            addGood(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              // this.$router.go()
            });
          }
        }
      });
    },
    buySubmitForm() {
      this.$refs["buyform"].validate(valid => {
        if (valid) {
          this.buyform.counts = Number(this.buyform.counts)
          buyGood(this.buyform).then(response => {
            this.$modal.msgSuccess("采购数量添加成功");
            this.buyopen = false;
            this.buyform = {};
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除供应商商品：' + row.goodName).then(function () {
        return delGood(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fx/supplier/good/export', {
        ...this.queryParams
      }, `good_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
